package apps.sim.proxy;

import java.net.ServerSocket;
import java.net.Socket;

public class ProxyServer extends Thread {

	public static boolean FLAG_RESOLVE_ADDRESSES = false;
	public static final int DEFAULT_PORT = 19999;
	public static final int DEFAULT_TIMEOUT = 20 * 1000;
	private ServerSocket server = null;

	public void doStart() {
		start();
	}

	public boolean isRunning() {
		return (server == null) ? false : true;
	}

	public void closeSocket() {
		try {
			server.close();
		} catch (Exception e) {
			//
		}
		server = null;
	}

	public void run() {
		try {
			server = new ServerSocket(DEFAULT_PORT);
			System.out.println("Started Proxy Server on port " + DEFAULT_PORT);
			while (true) {
				Socket client = server.accept();
				ProxyThread t = new ProxyThread(client);
				t.setTimeout(DEFAULT_TIMEOUT);
				t.start();
			}
		} catch (Exception e) {
			System.out.println("Msg: " + e.getMessage());
		}
		closeSocket();
	}

}
